<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>视频详情</title>
    <link rel="stylesheet" href="http://at.alicdn.com/t/font_2384888_ojge9md3tr.css
    ">
    <link rel="shortcut icon" href="../favicon.ico" type="image/x-icon">
    <link rel="stylesheet" href="../css/online.css">
</head>

<body>
    <!-- 导航菜单 -->
    <nav>
        <div class="main">
            <img src="../img/public/logo.png" alt="" class="logo">
            <ul>
                <li><a href="../index.html">首页</a></li>
                <li class="check"><a href="javascript:;">同步课程</a></li>
                <li><a href="javascript:;">精品课程</a></li>
            </ul>
            <div class="search">
                <div class="left">课程<i class="iconfont icon-down"></i>
                </div>
                <input type="text" placeholder="搜索感兴趣的内容">
                <img src="../img/public/search.png" alt="">
            </div>
            <div class="login">
                <a href="javascript:;">登陆</a>
                /
                <a href="javascript:;">注册</a>
            </div>
            <div class="user">
                <img src="../img/login/user.png" alt="">
                <div class="userlist">
                    <ul>
                        <li>我是用户名</li>
                        <li>课程中心</li>
                        <li>订单中心</li>
                        <li>资金管理</li>
                        <li>个人中心</li>
                        <li class="out">退出登陆</li>
                    </ul>
                </div>
            </div>
        </div>
    </nav>

    <!-- 主体内容 -->
    <main>
        <div class="row">
            <div class="left">热门城市</div>
            <div class="right">
                <ul id="areaContainer" class="filterUl">
                    <li><a t="area" v="不限">不限</a></li>
                    <!--<li><a>北京</a></li>
                    <li><a>上海</a></li>
                    <li><a>江苏</a></li>
                    <li><a>济南</a></li>
                    <li><a>河南</a></li>
                    <li><a>天津</a></li>
                    <li><a>沈阳</a></li>
                    <li><a>石家庄</a></li> -->
                </ul>
            </div>
        </div>
        <div class="row">
            <div class="left">按年级选择</div>
            <div class="right">
                <ul id="gradeContainer" class="filterUl">
                    <li><a t="grades" v="不限">不限</a></li>
                    <!-- <li><a>一年级</a></li>
                    <li><a>二年级</a></li>
                    <li><a>三年级</a></li>
                    <li><a>四年级</a></li>
                    <li><a>五年级</a></li>
                    <li><a>六年级</a></li>
                    <li><a>初一</a></li>
                    <li><a>初二</a></li>
                    <li><a>初三</a></li>
                    <li><a>高一</a></li>
                    <li><a>高二</a></li>
                    <li><a>高三</a></li> -->
                </ul>
            </div>
        </div>
        <div class="row">
            <div class="left">按学科选择</div>
            <div class="right">
                <ul id="subjectsContainer" class="filterUl">
                    <li><a t="subject" v="不限">不限</a></li>
                    <!-- <li><a>语文</a></li>
                    <li><a>数学</a></li>
                    <li><a>英语</a></li>
                    <li><a>生物</a></li>
                    <li><a>历史</a></li>
                    <li><a>政治</a></li>
                    <li><a>物理</a></li> -->
                </ul>
            </div>
        </div>
        <div class="row">
            <div class="left">按类别选择</div>
            <div class="right">
                <ul id="catesContainer" class="filterUl">
                    <li><a t="category" v="不限">不限</a></li>
                    <!-- <li><a class="active">期中</a></li>
                    <li><a>期末</a></li>
                    <li><a>小升初</a></li> -->
                </ul>
            </div>
        </div>
        <div class="lesson">
            <ul id="courseContainer">
                <!-- <li>
                    <div class="top">
                        <img src="../img/online/banner1-1.png" alt="" class="m">
                        <p>学科：英语</p>
                    </div>
                    <div class="bottom">
                        <div class="left">
                            <span>人教版语文第一节课程内容讲解</span>
                            <span class="time">地区：北京</span>
                        </div>
                        <div class="right">免费学习</div>
                    </div>
                </li> -->
            </ul>
        </div>

        <div class="pageNum">
            <!-- <button>上一页</button> -->
            <ul id="pageContainer" class="filterUl">
                <!-- <li class="active">1</li>
                <li>2</li>
                <li>3</li> -->
            </ul>
            <!-- <button class="active">下一页</button> -->
        </div>
    </main>

    <footer>
        <div class="cont">
            <div class="main">
                <p>
                    首页 | 关于我们 | 加入我们 | 合作专区 | 联系我们 | AI开放平台 | 意见反馈 | 漏洞提交 | 隐私政策 | 版权声明 | 反盗链声明 | 网上有害信息举报 | 备案公示 | 营业执照
                    | 教师资格证公示

                </p>
                <p>
                    京ICP备 13030888号 Copyright © 2014-2019 行者信息科技（北京）有限公司 | 地址：北京市新华区马当路388号C座 | 电话：010-66666666 | 京公网安备
                    01010102002533号
                </p>
                <p>
                    京网文〔2018〕4086-308号 | 网络文化经营许可证：沪网文[2018]4086-308号 | 增值电信业务经营许可证：京B2-20150021 |
                    食品经营许可证：JY13101140088888
                </p>
                <p>医疗器械经营许可证：京嘉食药监械经营许20188008号 | 互联网药品信息服务资格证书：(京)-经营性-2018-0011 |</p>
            </div>
        </div>
    </footer>



    <!-- <script src="../js/ujiuye.js"></script> -->
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script src="../utils/http.js"></script>

    <script>
        //地区
        async function getarea() {
            let res = await Http({
                url: '/api/areas'
            })

            // console.log(res);

            let area = res.result.result

            // console.log(area);

            let areaStr = ''

            area.forEach(item => {
                areaStr += `<li><a t="area" v="${item.area_name}">${item.area_name}</a></li>`
            });

            $('#areaContainer').append(areaStr)
        }
        getarea()


        //年级
        async function getgrades() {
            let res = await Http({
                url: '/api/grades'
            })

            // console.log(res);

            let grades = res.result.result

            // console.log(area);

            let gradesStr = ''

            grades.forEach(item => {
                gradesStr += `<li><a t="grades" v="${item.grade_name}">${item.grade_name}</a></li>`
            });

            $('#gradeContainer').append(gradesStr)
        }
        getgrades()


        //学科
        async function getsubjects() {
            let res = await Http({
                url: '/api/subjects'
            })

            // console.log(res);

            let subjects = res.result.result

            // console.log(area);

            let subjectsStr = ''

            subjects.forEach(item => {
                subjectsStr += `<li><a t="subject" v="${item.subject_name}">${item.subject_name}</a></li>`
            });

            $('#subjectsContainer').append(subjectsStr)
        }
        getsubjects()


        //类别
        async function getcates() {
            let res = await Http({
                url: '/api/cates'
            })

            // console.log(res);

            let cates = res.result.result

            // console.log(area);

            let catesStr = ''

            cates.forEach(item => {
                catesStr += `<li><a t="category" v="${item.category_name}">${item.category_name}</a></li>`
            });

            $('#catesContainer').append(catesStr)
        }
        getcates()


        //获取课程列表
        let filterData = { page: 1, pageSize: 5, area:'', subject:'', grades:'', category: '' }
        async function getCourses() {
            let { page, pageSize, area, subject, grades, category } = filterData
            //    console.log(page,pageSize,area,subject,grades,category);


            let res = await Http({
                url: '/api/courses',
                data: {
                    page,
                    pageSize,
                    area,
                    subject,
                    grades,
                    category
                }
            })

            // console.log(res);
            //  console.log(res);
            let courses = res.result
            //  console.log(courses);

            let coursesStr = ''

            courses.data.forEach(item => {
                coursesStr += ` <li>
                    <div class="top">
                        <img src="${item.image_src}" alt="" class="m">
                        <p>学科：${item.subject_name}</p>
                    </div>
                    <div class="bottom">
                        <div class="left">
                            <span>${item.title}</span>
                            <span class="time">地区：${item.area_name}</span>
                        </div>
                        <div class="right">${item.price == 0 ? '免费学习' : item.price + '元'}</div>
                    </div>
                </li>`
            })

            $('#courseContainer').html(coursesStr)

            setPage(courses.totalPage, courses.page)
        }
        getCourses()


        //处理页码
        function setPage(totalPage, page) {
            // console.log(totalPage);
            let next=page+1
            if(next>=totalPage){
                  next=totalPage
            }
            let prev=page-1
            if(prev==0){
                prev=1
            }
            pageStr = `<button t="page" v="${prev}">上一页</button>`
            for (let i = 1; i <= totalPage; i++) {
                pageStr += ` <li class='${i == page ? 'active' : ''}'><a t="page" v="${i}">${i}</a></li>`
            }

            pageStr+=`<button t="page" v="${next}">下一页</button>`
            $('#pageContainer').html(pageStr)
        }


        //为地区加入点击事件
        //通过ajax拿到的手机然后添加上到页面的元素不能绑定事假,要用事件委托解决
        $('.filterUl').on('click', 'a,button', function () {
            $(this).addClass('active')
            // $(this).parents('li').addClass('')
            // console.log($(this).parents('li').siblings().find('a'));
             $(this).parents('li').siblings().find('a').removeClass('active');
            // 有多个条件，要知道是属于哪一类
            let value = $(this).attr('v')
            let type = $(this).attr('t')
            if ( value == '不限' ) {
                value=''
            }
            filterData[type] = value
            console.log(filterData);
            // console.log(filterData[type]);
            //    console.log(value);
            //    return 
            //    console.log({[type]:value});
            getCourses()

        })






    </script>
</body>

</html>